﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using _52fzc.Model.PattenModel;

namespace _52fzc.Model.HttpModel
{
    public class SessionModel : BaseEntity
    {
        protected HttpSessionState _session;
        protected Context _context;
        protected string _domain="";
        protected string _sep = "";
        protected int _domainIndexLength = 12;

        public SessionModel(HttpSessionState session)
        {
            _session = session;
            _domain = "";
            _sep = "";
        }
        
        public SessionModel(HttpSessionState session, Context context)
        {
            _session = session;
            _context = context;
            _sep = "";
        }

        public SessionModel(HttpSessionState session,string domain)
        {
            _session = session;
            _domain = domain;
            _sep = "_";
        }

        public SessionModel(HttpSessionState session, Context context, string domain)
        {
            _session = session;
            _context = context;
            _domain = domain;
            _sep = "_";
        }

        public virtual object Get(string paramName, object defaultVal)
        {
            string index = "";
            index = _domain.ToLower() + _sep + paramName.ToLower();
            if (_session[index] != null)
            {
                return _session[index];
            }
            else
            {
                return defaultVal;
            }
        }

        public virtual object Get(string paramName)
        {
            return Get(paramName, null);
        }

        public virtual string GetString(string paramName, string defaultVal)
        {
            return (Get(paramName, defaultVal)).ToString();
        }

        public virtual string GetString(string paramName)
        {
            return GetString(paramName, "");
        }

        public virtual int GetInt(string paramName, int defaultVal)
        {
            return Convert.ToInt32(Get(paramName, defaultVal));
        }

        public virtual int GetInt(string paramName)
        {
            return GetInt(paramName, 0);
        }

        public virtual float GetFloat(string paramName, float defaultVal)
        {
            return (float)Get(paramName, defaultVal);
        }

        public virtual float GetFloat(string paramName)
        {
            return GetFloat(paramName, 0);
        }

        public virtual double GetDouble(string paramName, double defaultVal)
        {
            return Convert.ToDouble(Get(paramName, defaultVal));
        }

        public virtual double GetDouble(string paramName)
        {
            return GetDouble(paramName, 0);
        }

        public virtual void Put(string paramName, object paramVal)
        {
            string index = "";
            index = _domain.ToLower() + _sep + paramName.ToLower();
            if (_session[index] != null)
            {
                _session[index] = paramVal;
            }
            else
            {
                _session.Add(index, paramVal);
            }
        }

        public virtual void Clear()
        {
            _session.Clear();
        }

        public virtual void Clear(string domain)
        {
            for (int i = 0; i < _session.Keys.Count; i++)
            {
                if (_session.Keys[i].IndexOf(domain.ToLower() + _sep) == 0)
                {
                    _session.Remove(_session.Keys[i]);
                }
            }
        }

        public virtual bool GetBoolean(string paramName, bool defaultVal)
        {
            return (bool)(Get(paramName, defaultVal));
        }

        public virtual bool GetBoolean(string paramName)
        {
            return (bool)(Get(paramName, false));
        }

        public virtual void Remove(string paramName)
        {
            _session.Remove(paramName.ToLower());
        }

        public virtual string Domain
        {
            get
            {
                return _domain;
            }
        }
    }
}
